package com.chuangyang.fixboxmaster.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.bumptech.glide.Glide;
import com.chuangyang.fixboxlib.utils.StringUtils;
import com.chuangyang.fixboxlib.widgets.ListItemView;
import com.chuangyang.fixboxlib.widgets.RatingBar;
import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.CashInfo;
import com.chuangyang.fixboxmaster.bean.FirstNameEvent;
import com.chuangyang.fixboxmaster.bean.User;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.CashRequest2Activity;
import com.chuangyang.fixboxmaster.ui.MessageCenterActivity;
import com.chuangyang.fixboxmaster.ui.ModuleActivity;
import com.chuangyang.fixboxmaster.ui.MoreSettingActivity;
import com.chuangyang.fixboxmaster.ui.widgets.BadgeView;
import com.chuangyang.fixboxmaster.utils.Constants;

import de.greenrobot.event.EventBus;


public class ProfileFragment extends BaseFragment implements View.OnClickListener {
    private View mContentView;
    private ImageView mAvatar;
    private TextView mUserName;
    private ImageView mUserLevel;
    private TextView mNumberId;
    private TextView mPhoneNumber;
    private TextView mBalance;
    private RatingBar mRatingStar;

    private TextView mMonthIntegration;
    private TextView mTotalIntegration;
    private CashInfo cashInfo = new CashInfo();
    private BadgeView mBadgeView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_profile, container, false);
        mAvatar = (ImageView) mContentView.findViewById(R.id.user_avatar);
        mUserName = (TextView) mContentView.findViewById(R.id.user_name);
        mNumberId = (TextView) mContentView.findViewById(R.id.number_id);
        mPhoneNumber = (TextView) mContentView.findViewById(R.id.phone_number);
        mRatingStar = (RatingBar) mContentView.findViewById(R.id.master_star);
        mBalance = (TextView) mContentView.findViewById(R.id.profile_master_balance);
        mUserLevel = (ImageView) mContentView.findViewById(R.id.user_level);
        ListItemView mMyOrder = (ListItemView) mContentView.findViewById(R.id.btn_my_order);
        LinearLayout mCashRequest = (LinearLayout) mContentView.findViewById(R.id.btn_cash_request);
        LinearLayout mMessageCenter = (LinearLayout) mContentView.findViewById(R.id.btn_message_center);
        ListItemView mSkillRequest = (ListItemView) mContentView.findViewById(R.id.btn_skill_request);
        ListItemView mMoreSetting = (ListItemView) mContentView.findViewById(R.id.btn_more_setting);
        ListItemView mCallCustomService = (ListItemView) mContentView.findViewById(R.id.call_custom_service);
        mMonthIntegration=(TextView)mContentView.findViewById(R.id.month_integration);
        mTotalIntegration=(TextView)mContentView.findViewById(R.id.all_integration);
        mBadgeView=(BadgeView)mContentView.findViewById(R.id.bd_unread);
        mMyOrder.setOnClickListener(this);
        mCashRequest.setOnClickListener(this);
        mSkillRequest.setOnClickListener(this);
        mMoreSetting.setOnClickListener(this);
        mCallCustomService.setOnClickListener(this);
        mMessageCenter.setOnClickListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
    }

    @Override
    public void onResume() {
        super.onResume();
        addRequest(new GsonRequest<>(FixBoxApi.USER_INFO, User.UserInfo.class, responseListener));
    }

    private Response.Listener<User.UserInfo> responseListener = new Response.Listener<User.UserInfo>() {
        @Override
        public void onResponse(User.UserInfo response) {
            if (getActivity() != null && isAdded()){
                if (response != null && response.user != null) {
                    showResult();
                    if (response.user.media != null) {
                        Glide.with(AppData.getContext()).load(response.user.media.url).centerCrop().crossFade().
                                placeholder(R.drawable.ic_default_avatar).into(mAvatar);
                    }
                    mUserName.setText(response.user.firstName + response.user.lastName);
                    FirstNameEvent firstNameEvent = new FirstNameEvent();
                    firstNameEvent.firstName = response.user.firstName;
                    firstNameEvent.workState = response.user.workState;
                    firstNameEvent.notreadTotal=response.user.notreadTotal;
                    EventBus.getDefault().postSticky(firstNameEvent);

                    cashInfo.cardNum = response.user.bankCardNo;
                    cashInfo.cardAddress = response.user.bankPlace;
                    cashInfo.cash = String.valueOf(response.user.cash);
                    cashInfo.balance = String.valueOf(response.user.balance);
                    cashInfo.accountName = response.user.userName;
                    cashInfo.orderDoneNumber = response.user.orderCount;
                    cashInfo.todayBalance = response.user.todayBalance;
                    cashInfo.totalBalance = response.user.totalBalance;
                    cashInfo.monthIntegration=response.user.monthIntegrals;
                    cashInfo.totalIntegration=response.user.totaIntegrals;
                    if (response.user.level == 10) {
                        mUserLevel.setVisibility(View.VISIBLE);
                    }
                    mNumberId.setText(getString(R.string.profile_master_id, response.user.jobId));
                    mPhoneNumber.setText(getString(R.string.profile_master_phone_number, response.user.phoneNumber));
                    mBalance.setText(getString(R.string.profile_master_balance, (int) response.user.balance));
//                    mCallCustomService.setImageText(getString(R.string.customer_service_number, Constants.COSTOM_SERVICE_NUMBER));
//                    mCallCustomService.setImageTextColor(AppData.getContext().getResources().getColor(R.color.blue_background));
                    mRatingStar.setRating(StringUtils.getNumStar(response.user.rate));
                    mMonthIntegration.setText(String.valueOf(response.user.monthIntegrals));
                    mTotalIntegration.setText(String.valueOf(response.user.totaIntegrals));
                    mBadgeView.setText(""+response.user.notreadTotal);
                    if(response.user.notreadTotal<=0){
                        mBadgeView.setVisibility(View.GONE);
                    }else {
                        mBadgeView.setVisibility(View.VISIBLE);
                    }


                } else
                    setEmptyView();
            }
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_my_order:
                Intent intent = new Intent(getActivity(), ModuleActivity.class);
                intent.putExtra(ModuleActivity.TYPE, ModuleActivity.MY_ORDER_LIST);
                startActivity(intent);
                break;
            case R.id.btn_cash_request:
                Intent intentCashRequest = new Intent(getActivity(), CashRequest2Activity.class);
                intentCashRequest.putExtra("cashInfo", cashInfo);
                startActivity(intentCashRequest);
                break;
            case R.id.btn_skill_request:
                Intent intentSkill = new Intent(getActivity(), ModuleActivity.class);
                intentSkill.putExtra(ModuleActivity.TYPE, ModuleActivity.MY_SKILL_ACTIVITY);
                startActivity(intentSkill);
                break;
            case R.id.btn_more_setting:
                startActivity(new Intent(getActivity(), MoreSettingActivity.class));
                break;
            case R.id.call_custom_service:
                StringUtils.dialPhone(getActivity(), Constants.COSTOM_SERVICE_NUMBER);
                break;
            case R.id.btn_message_center:
                startActivity(new Intent(getActivity(), MessageCenterActivity.class));
                break;
        }
    }
}
